package cn.cttic.auth.controller;

import cn.cttic.auth.form.MaLoginBody;
import cn.cttic.auth.service.AppLoginService;
import cn.cttic.common.core.domain.R;
import cn.cttic.common.core.domain.model.LoginBody;
import com.alibaba.nacos.api.common.Constants;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 移动端 - 鉴权
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app")
public class MaController {

    private final AppLoginService appLoginService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public R<Map<String, Object>> login(@Validated @RequestBody MaLoginBody form) {
        // 用户登录
        String accessToken = appLoginService.login(form.getUsername(), form.getPassword());

        // 接口返回信息
        Map<String, Object> rspMap = new HashMap<>();
        rspMap.put(Constants.ACCESS_TOKEN, accessToken);
        return R.ok(rspMap);
    }

    /**
     * 登出
     */
    @PostMapping("logout")
    public R<Void> logout() {
        appLoginService.logout();
        return R.ok();
    }


}
